package part1;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;

/**
 * Main3WriteReadCompare
 *  - Write to a text file, read it back in, check if the two are the same
 * @author tony
 *
 */

public class Main3WriteReadCompare {
	public static void main(String[] args) {
		final String OUTPUTFILENAME = "output.txt";
		Data d1 = new Data(10, "Hello");
		System.out
				.println("In this program, first we will write the data members of an object to the file "	+ OUTPUTFILENAME);

		try {
			PrintWriter pw = new PrintWriter(OUTPUTFILENAME);
			pw.println(d1.getX());
			pw.println(d1.getY());
			pw.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		System.out.println("   OK, done writing - refresh your project to see the file.");

		final String INPUTFILENAME = OUTPUTFILENAME;
		Data d2 = null;
		System.out.println("Next we will read data from the file " + INPUTFILENAME);

		Scanner read;
		try {
			read = new Scanner(new File(INPUTFILENAME));
			int x = read.nextInt();
			String y = read.next();
			read.close();

			d2 = new Data(x, y);
			System.out.println("   OK, done reading. The file had: x = " + x + ", and y = " + y);

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}

		if (d1.equals(d2))
			System.out.println("The input is the same as the output");
		else
			System.out.println("The input is not the same as the output");
	}

}
